%{
    This file is part of StemCellQC, a video bioinformatics software
    toolkit for analysis of phase contrast microscopy videos.
    Copyright 2013-2015 Vincent On. [vincenton001-at-gmail.com]

    StemCellQC is free software: you can redistribute it and/or 
    modify it under the terms of the GNU General Public License as 
    published by the Free Software Foundation, either version 3 of the 
    License, or (at your option) any later version.

    StemCellQC is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with StemCellQC.  If not, see <http://www.gnu.org/licenses/>.
%}

folder_name = uigetdir('','Select Folder of Extracted Data files');
pastDir = cd;
cd( folder_name );

% the list of files in the directory
list = ls;

% remove '.' and '..' from list
index = find( list( : , 1 ) == '.' );
list( index, : ) = [];

cd( pastDir );

%load all files in directory
hWaitbar = waitbar( 0, 'Loading data' );

interval = round( n_frames/2 );
for i = 1 : size( list , 1 )
    
    temp = xlsread( [folder_name '\' deblank( list(i,:))]);
    temp( isnan(temp) ) = 0;
    
    %fix some bad data points
    for j = 2 : ( length( temp( : , 1 ) ) - 1 )
        if temp( j , end ) > 200
            if temp( j+1 , end ) > 200
                temp( j , : ) = mean( [ temp( j-1 , : ); temp( j+1 , : ) ] );
            end
        end
    end
    
    temp(:,1) = [];  %remove index column
    
    %Derive additional features
    temp = derive_features( temp );
    
    %remove unwanted columns or features
    remove_c = 1 : length( temp(1,:) );
    remove_c( features ) = [];
    
    
    temp( : , remove_c ) = [];
    
    %multiple inverse of feature multipler for weights
%     for j = 1 : length( multipler_value );
%         temp( : , j ) = ( 1 / 1 ) * temp( : , j );
%     end
    
    data( : , : , i ) = temp;
    
    close( hWaitbar );
    
    hWaitbar = waitbar( i / size(list,1), ...
        [ 'Loading data' ] );
    
    %calculate moving average
    for j = 1 : length( data( : , 1 , 1 ) )
        indices = j - interval : j + interval;
        indices( indices < 1 ) = [];
        indices( indices > length( data( :,1,1 ) ) ) = [];
        
        avg_data( j,:,i ) = mean( data( indices,:,i ) );
    end
end

close( hWaitbar );

%reshape array
temp = [];
samples = [];

%normalize data

maxs = max( max( data,[],3 ) , [] , 1);
mins = min( min( data,[],3 ) , [] , 1);

[N_tp, N_feats, N_samples] = size( data );

for i = 1 : N_feats
    data(:,i,:) = (data(:,i,:)-mins(i))./(maxs(i)-mins(i));
end

%Calculate slopes
mod_features = [];
for k = 1 : N_feats
    %take current feature
    for i = 1 : N_samples
        Feature(i,:) = data(:,k,i);
    end
    
    %find slope of features
    for i = 1:(length(Feature(1,:))-1)
        Slopes_F(:,i) = (Feature(:,i+1)-Feature(:,i))/2;
    end
    
    [input_value, std_value] = gen_input(Slopes_F, Feature, 'avg slope', 40);
    input_value = (input_value-min(min(input_value)))./(max(max(input_value))-min(min(input_value)));
    
    mod_features = [ mod_features input_value ];
end

mod_features = (mod_features-min(min(mod_features)))./(max(max(mod_features))-min(min(mod_features)));

%reshape data
samples = mod_features;

%clean for classifiers
samples( isnan( samples ) ) = 0;
samples(find(sum(abs(samples))==0),:) = [];

%load labels

[name_file, path] = uigetfile('*.*','Load Excel File of Labels');
file = [path name_file];

labels = xlsread( file );
